package com.sky.controller.admin;

import com.sky.Service.Impl.SetmealServiceImpl;
import com.sky.Service.SetmealService;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@Api(tags = "套餐管理")
@RestController(value = "adminSetmealController")
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealServiceImpl setmealServiceImpl;

    @ApiOperation("获取套餐列表")
    @GetMapping("/page")
    public Result<PageResult> getByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        System.out.println("setmealPageQueryDTO:" + setmealPageQueryDTO);
        PageResult byPage = setmealService.getByPage(setmealPageQueryDTO);
        return Result.success(byPage);
    }

    //精确处理缓存
    //如果新增套餐的话,就把该套餐所属的分类的缓存清空
    @CacheEvict(cacheNames="setmealCache",key="#setmealDTO.categoryId")
    @ApiOperation("新增套餐信息")
    @PostMapping
    public Result<String> addSetmealInfo(@RequestBody SetmealDTO setmealDTO) {
        System.out.println("setmealDTO:" + setmealDTO);
        setmealService.addSetmealInfo(setmealDTO);
        return Result.success("新增套餐信息成功");
    }

    @CacheEvict(cacheNames="setmealCache",allEntries=true)
    @ApiOperation("修改套餐状态")
    @PostMapping("/status/{status}")
    public Result<String> updateSetmealStatus(@PathVariable Integer status, Long id) {
        System.out.println("status:" + status);
        System.out.println("id:" + id);
        setmealService.updateSetmealStatus(status, id);
        return Result.success("修改套餐状态成功");
    }

    @ApiOperation("根据id查询套餐信息")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id) {
        System.out.println("id:" + id);
        SetmealVO setmealById = setmealService.getSetmealById(id);
        return Result.success(setmealById);
    }

    @CacheEvict(cacheNames="setmealCache",allEntries=true)
    @ApiOperation("修改套餐信息")
    @PutMapping
    public Result<String> modiftSetmealInfo(@RequestBody SetmealVO setmealVO){
        System.out.println("setmealVO:" + setmealVO);
        setmealService.modifySetmealInfo(setmealVO);
        return Result.success("修改套餐信息成功");

    }

    @CacheEvict(cacheNames="setmealCache",allEntries=true)
    @ApiOperation("删除套餐信息")
    @DeleteMapping
    public Result<String> deleteSetmealByIds(Long[] ids){
        for(Long id : ids){
            System.out.println("id:" + id);
        }
        setmealService.deleteSetmealByIds(ids);
        return Result.success("删除套餐信息成功");

    }

}
